<?php
namespace app\common\model;
use think\facade\Session;

/**
 * 用户模型
 */
class User extends BaseModel
{
    // 定义表名
    protected $name = 'user';
    // 定义主键
    protected $pk = 'user_id';
    // 追加字段
    protected $append = [];

    /**
     * 用户资料
     */
    public function detail()
    {
        return $this->hasOne('app\\common\\model\\UserDetail');
    }
    /**
     * 用户头像
     */
    public function getAvatarAttr($value)
    {
        empty($value) && $value = base_url() . 'assets/img/avatar.png';
        return $value;
    }
    /**
     * 显示性别
     */
    public function getGenderAttr($value)
    {
        $status = [0 => '未知', 1 => '先生', 2 => '女士'];
        return $status[$value];
    }
    
    /**
     * 用户身份
     */
    public function getStatusAttr($value)
    {
        $status = [10 => '管理', 20 => '用户', 30 => '代理'];
        return ['text' => $status[$value], 'value' => $value];
    }
    
    /**
     * 获取列表
     */
    public function getList($status = 20, $agent_id = 0, $gender = 'all', $search = '')
    {
        $model = $this->order('user_id','desc');
        $search = trim($search);
        $filter = [];
        $gender != 'all' && $filter['gender'] = $gender;
        if(!empty($search)){
            //是否是数字
            if(is_numeric($search)){   
                //是否是手机号
                if(is_phone($search)){
                    $filter['phone'] = $search;
                }else{
                    $filter['user_id'] = $search;
                }
            }else{
               //不是数字   
                $model->where('nickname','like',"%{$search}%");
            }
        }
        //代理获取用户
        if($agent_id > 0){
            $filter['agent_id'] = $agent_id;
            $model->where('status','>',10);
        }else{
            if($status == 20){
                $model->where('status','>',10);
            }else{
                $filter['status'] = $status;
            }
        }
        // 执行查询
        return $model->with(['detail'])
            ->where($filter)
            ->paginate(['list_rows'=>15,'query' => request()->param()]);
    }
    /**
     * 获取详情信息 - 跟据条件
    */
    public static function getUser(array $filter)
    {
        return self::where($filter)->with(['detail'])->find();
    }
    
    /**
     * 编辑
    */
    public function edit(array $data)
    {
        return $this->save($data) != false;
    }
    
     /**
     * 修改管理员密码
     */
    public function renew(array $data)
    {
        //验证密码长度是否合法
        if(strlen($data['password'])<4){
            $this->error = '新密码长度不足4位';
            return false;
        }
        if ($data['password'] !== $data['password_confirm']) {
            $this->error = '两次输入的新密码不一致';
            return false;
        }
        // 更新管理员信息
        return $this->save([
            'password' => hema_hash($data['password'])
        ]) !== false;
    }

    /**
     * 一键登录
     */
    public function oneKey($status='')
    {
        if($this->status['value'] == 10){
            $this->error = '非法用户';
            return false;
        }else{
            $name = 'hema_user';
            $url = '/user';
            if($status == 30){
                $name = 'hema_agent';
                $url = '/agent';
            }
        }
        Session::set($name, [
            'user' => $this,
            'is_login' => true,
        ]);
        return $url;
    }
   
    /**
     * 根据条件统计数量
     */
    public static function getCount($agent_id = 0)
    {
        // 筛选条件
        $filter = [];
        $agent_id > 0 && $filter['agent_id'] = $agent_id;
        $model = self::where($filter);
        $count = array();
        //全部统计
        $count['all'] = [
            'user' => $model->where(['status' => 20])->count(),//全部用户数量
            'agent' => $model->where(['status' => 30])->count(),//全部代理数量
            'money' => [
                'user' => $model->where(['status' => 10])->sum('money'),
                'agent' => $model->where(['status' => 30])->sum('money'),//代理余额
            ],
        ];
        //今天统计
        $star = strtotime(date('Y-m-d 00:00:00',time()));
        $count['today'] = [
            'user' => $model->where('create_time','>',$star)->where(['status' => 20])->count(),//全部用户数量
            'agent' => $model->where('create_time','>',$star)->where(['status' => 30])->count(),//全部代理数量
            'money' => [
                'user' => $model->where('create_time','>',$star)->where(['status' => 10])->sum('money'),
                'agent' => $model->where('create_time','>',$star)->where(['status' => 30])->sum('money'),//代理余额
            ],
        ];
        //昨天统计
        $star = strtotime("-1 day");
        $end = strtotime(date('Y-m-d 00:00:00',time()));
        $count['today2'] = [
            'user' => $model->where('create_time','>',$star)->where('create_time','<',$end)->where(['status' => 20])->count(),//全部用户数量
            'agent' => $model->where('create_time','>',$star)->where('create_time','<',$end)->where(['status' => 30])->count(),//全部代理数量
            'money' => [
                'user' => $model->where('create_time','>',$star)->where('create_time','<',$end)->where(['status' => 10])->sum('money'),
                'agent' => $model->where('create_time','>',$star)->where('create_time','<',$end)->where(['status' => 30])->sum('money'),//代理余额
            ],
        ];
        //前天统计
        $star = strtotime("-2 day");
        $end = strtotime("-1 day");
        $count['today3'] = [
            'user' => $model->where('create_time','>',$star)->where('create_time','<',$end)->where(['status' => 20])->count(),//全部用户数量
            'agent' => $model->where('create_time','>',$star)->where('create_time','<',$end)->where(['status' => 30])->count(),//全部代理数量
            'money' => [
                'user' => $model->where('create_time','>',$star)->where('create_time','<',$end)->where(['status' => 10])->sum('money'),
                'agent' => $model->where('create_time','>',$star)->where('create_time','<',$end)->where(['status' => 30])->sum('money'),//代理余额
            ],
        ];
        //-4天统计
        $star = strtotime("-3 day");
        $end = strtotime("-2 day");
        $count['today4'] = [
            'user' => $model->where('create_time','>',$star)->where('create_time','<',$end)->where(['status' => 20])->count(),//全部用户数量
            'agent' => $model->where('create_time','>',$star)->where('create_time','<',$end)->where(['status' => 30])->count(),//全部代理数量
            'money' => [
                'user' => $model->where('create_time','>',$star)->where('create_time','<',$end)->where(['status' => 10])->sum('money'),
                'agent' => $model->where('create_time','>',$star)->where('create_time','<',$end)->where(['status' => 30])->sum('money'),//代理余额
            ],
        ];
        //-5天统计
        $star = strtotime("-4 day");
        $end = strtotime("-3 day");
        $count['today5'] = [
            'user' => $model->where('create_time','>',$star)->where('create_time','<',$end)->where(['status' => 20])->count(),//全部用户数量
            'agent' => $model->where('create_time','>',$star)->where('create_time','<',$end)->where(['status' => 30])->count(),//全部代理数量
            'money' => [
                'user' => $model->where('create_time','>',$star)->where('create_time','<',$end)->where(['status' => 10])->sum('money'),
                'agent' => $model->where('create_time','>',$star)->where('create_time','<',$end)->where(['status' => 30])->sum('money'),//代理余额
            ],
        ];
        //-6天统计
        $star = strtotime("-5 day");
        $end = strtotime("-4 day");
        $count['today6'] = [
            'user' => $model->where('create_time','>',$star)->where('create_time','<',$end)->where(['status' => 20])->count(),//全部用户数量
            'agent' => $model->where('create_time','>',$star)->where('create_time','<',$end)->where(['status' => 30])->count(),//全部代理数量
            'money' => [
                'user' => $model->where('create_time','>',$star)->where('create_time','<',$end)->where(['status' => 10])->sum('money'),
                'agent' => $model->where('create_time','>',$star)->where('create_time','<',$end)->where(['status' => 30])->sum('money'),//代理余额
            ],
        ];
        //-7天统计
        $star = strtotime("-6 day");
        $end = strtotime("-5 day");
        $count['today7'] = [
            'user' => $model->where('create_time','>',$star)->where('create_time','<',$end)->where(['status' => 20])->count(),//全部用户数量
            'agent' => $model->where('create_time','>',$star)->where('create_time','<',$end)->where(['status' => 30])->count(),//全部代理数量
            'money' => [
                'user' => $model->where('create_time','>',$star)->where('create_time','<',$end)->where(['status' => 10])->sum('money'),
                'agent' => $model->where('create_time','>',$star)->where('create_time','<',$end)->where(['status' => 30])->sum('money'),//代理余额
            ],
        ];
        //本月统计 
        $end = mktime(0,0,0,date('m'),1,date('y')); 
        $count['month2'] = [
            'user' => $model->where('create_time','>',$end)->where(['status' => 20])->count(),//全部用户数量
            'agent' => $model->where('create_time','>',$end)->where(['status' => 30])->count(),//全部代理数量
            'money' => [
                'user' => $model->where('create_time','>',$end)->where(['status' => 10])->sum('money'),
                'agent' => $model->where('create_time','>',$end)->where(['status' => 30])->sum('money'),//代理余额
            ],
        ];
        //上月统计  
        $star = mktime(0,0,0,date('m')-1,1,date('y'));
        $count['month2'] = [
            'user' => $model->where('create_time','>',$star)->where('create_time','<',$end)->where(['status' => 20])->count(),//全部用户数量
            'agent' => $model->where('create_time','>',$star)->where('create_time','<',$end)->where(['status' => 30])->count(),//全部代理数量
            'money' => [
                'user' => $model->where('create_time','>',$star)->where('create_time','<',$end)->where(['status' => 10])->sum('money'),
                'agent' => $model->where('create_time','>',$star)->where('create_time','<',$end)->where(['status' => 30])->sum('money'),//代理余额
            ],
        ];
        return $count;
    }
}